package com.hrms.controller.admin;

import com.hrms.entity.Employee;
import com.hrms.http.ResponseResult;
import com.hrms.service.EmployeeService;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

@Transactional
@RestController
@RequestMapping("/admin/employeemanage/editemployee")
public class AdminEditEmployeeController {
    // 管理员员工信息修改

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/init")
    public ResponseResult<List<Map<String, Object>>> init() {
        // 初始化
        List<Map<String, Object>> list = employeeService.selectAll();
        return new ResponseResult<>("SUCCESS", list);
    }

    @RequestMapping("/update")
    public ResponseResult<Object> update(@RequestBody EntityUtil entity) {
        // 修改员工信息
        Employee employee = new Employee();
        employee.setEmployee_id(entity.getEmployee_id());
        employee.setEmployee_name(entity.getEmployee_name());
        employee.setEmployee_password(entity.getEmployee_password());

        // 检查账号是否离线
        Employee employeeBefore = new Employee();
        employeeBefore.setEmployee_id(entity.getEmployee_id());
        employeeBefore.setEmployee_status("离线");

        if(!employeeService.checkStatue(employeeBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改
        if(!employeeService.updatebyAdmin(employee)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/delete")
    public ResponseResult<Object> delete(@RequestBody EntityUtil entity) {
        // 删除账号

        // 检查账号是否离线
        Employee employeeBefore = new Employee();
        employeeBefore.setEmployee_id(entity.getEmployee_id());
        employeeBefore.setEmployee_status("离线");

        if(!employeeService.checkStatue(employeeBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改账号状态
        Employee employeeAfter = new Employee();
        employeeAfter.setEmployee_id(entity.getEmployee_id());
        employeeAfter.setEmployee_status("已注销");

        if(!employeeService.updateStatue(employeeAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/logout")
    public ResponseResult<Object> logout(@RequestBody EntityUtil entity) {
        // 注销连接

        // 检查账号是否在线
        Employee employeeBefore = new Employee();
        employeeBefore.setEmployee_id(entity.getEmployee_id());
        employeeBefore.setEmployee_status("在线");

        if(!employeeService.checkStatue(employeeBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改账号状态
        Employee employeeAfter = new Employee();
        employeeAfter.setEmployee_id(entity.getEmployee_id());
        employeeAfter.setEmployee_status("离线");

        if(!employeeService.updateStatue(employeeAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/recover")
    public ResponseResult<Object> recover(@RequestBody EntityUtil entity) {
        // 恢复账号

        // 检查账号是否已注销
        Employee employeeBefore = new Employee();
        employeeBefore.setEmployee_id(entity.getEmployee_id());
        employeeBefore.setEmployee_status("已注销");

        if(!employeeService.checkStatue(employeeBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改账号状态
        Employee employeeAfter = new Employee();
        employeeAfter.setEmployee_id(entity.getEmployee_id());
        employeeAfter.setEmployee_status("离线");

        if(!employeeService.updateStatue(employeeAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }
}
